﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace MUnitGenerator.Helpers
{
    public class TestTargetHelper
    {
        public static TestTarget GetTargetUnderCursor(string text, int position)
        {
            TestTarget target = new TestTarget();
            target.type = TestTargetType.Nothing;

            {
                // Check for method

                int nextIndex = 0;
                int functionIndex = -1;

                while (true)
                {
                    int index = text.IndexOf("function", nextIndex);
                    if (index == -1)
                    {
                        break;
                    }
                    if (index > position)
                    {
                        break;
                    }
                    functionIndex = index;
                    nextIndex += "function".Length;
                    if (nextIndex >= text.Length)
                    {
                        break;
                    }
                }

                if (functionIndex != -1)
                {
                    Console.Write(text.Length + " " + functionIndex);
                    Group group = new Regex("function\\s+(\\w+)\\s*\\(")
                        .Match(text, functionIndex).Groups[1];


                    if (position >= group.Index && position <= group.Index + group.Value.Length)
                    {
                        target.MethodName = group.Value;
                        target.ClassName = null;
                        target.type = TestTargetType.Method;
                        return target;
                    }
                }
            }

            {
                // Check for class

                int classIndex = -1;
                int lastClassIndex = 0;

                while (true)
                {
                    int index = text.IndexOf("class", lastClassIndex);
                    if (index == -1)
                    {
                        break;
                    }
                    if (index > position)
                    {
                        break;
                    }
                    classIndex = index;
                    lastClassIndex = index + "class".Length;
                    if (lastClassIndex > text.Length)
                    {
                        break;
                    }
                }

                if (classIndex != -1)
                {
                    Group group = new Regex("class\\s+(\\w+)").Match(text, classIndex).Groups[1];
                    if (position >= group.Index && position <= group.Index + group.Value.Length)
                    {
                        target.ClassName = group.Value;
                        target.type = TestTargetType.Class;
                        return target;
                    }
                }
            }

            return target;
        }
    }
}
